package com.wangdao.mall.controller.admin;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.wangdao.mall.model.*;
import com.wangdao.mall.service.OrderService;
import com.wangdao.mall.service.OrderServiceImp;
import com.wangdao.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 18:50 2020/5/3
 * @Modified By:
 */
@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    private OrderService orderService = new OrderServiceImp();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/admin/order/", "");
        if ("ordersByPage".equals(action)){
            ordersByPage(request,response);
        }else if ("changeOrder".equals(action)){
            changeOrder(request,response);
        }
    }

    /**
     * 更新用户数据
     * @param request
     * @param response
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrderUpdate update = gson.fromJson(requestBody, OrderUpdate.class);
        int code = orderService.changeOrder(update);
        Result result = new Result();
        if (code == 200){
            result.setCode(0);
        }else if (code == 300){
            result.setCode(10000);
            result.setMessage("当前不能修改商品数量和规格");
        }else{
            result.setCode(10000);
            result.setMessage("订单状态修改异常");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 分页同时多条件查询显示订单
     * @param request
     * @param response
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrderRequestParam param = gson.fromJson(requestBody, OrderRequestParam.class);
        //将金额上下限转化为double数据
        Result result = new Result();
        try {
            if (!StringUtils.isEmpty(param.getMoneyLimit1())){
                Double.parseDouble(param.getMoneyLimit1());
            }
            if (!StringUtils.isEmpty(param.getMoneyLimit2())){
                Double.parseDouble(param.getMoneyLimit2());
            }
        } catch (NumberFormatException e) {
            //异常说明数据不能转为double数据
            result.setCode(10000);
            result.setMessage("数据异常，请重新输入");
            response.getWriter().println(gson.toJson(result));
            return;
        }
        OrderResponse order = orderService.ordersByPage(param);
        result.setCode(0);
        result.setData(order);
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/admin/order/", "");
        if ("order".equals(action)){
            getorder(request,response);
        }else if ("deleteOrder".equals(action)){
            deleteOrder(request,response);
        }
    }

    /**
     * 删除订单
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        int row = orderService.deleteOrder(id);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("删除失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 得到订单的数据
     * @param request
     * @param response
     * @throws IOException
     */
    private void getorder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<State> states = new ArrayList();
        states.add(new State(0,"未付款"));
        states.add(new State(1,"未发货"));
        states.add(new State(2,"已发货"));
        states.add(new State(3,"已完成订单"));
        OrderInfo orderInfo = orderService.getOrder(id);
        orderInfo.setStates(states);
        Result result = new Result();
        result.setCode(0);
        result.setData(orderInfo);
        response.getWriter().println(gson.toJson(result));
    }
}
